package com.maduar.demo.algo.interview;

import java.util.Stack;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-03-18 00:24
 */
public class MiddleInterview {

    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode res = new ListNode(-1);
        ListNode list = res;
        Stack<Integer> stack = new Stack<>();
        while(head != null) {
            int current = head.val;
            if(current == left) {
                stack.push(current);
            } else if(current == right) {
                int len = stack.size();
                for(int i = 0; i < len; i++) {
                    list.next = new ListNode(stack.pop());
                }
            } else{
                list.next = head;
                list = list.next;
            }
            head = head.next;
        }

        return res.next;
    }
}
